﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLTB.Controllers;
using QuanLyThietBi.Manager;
using QuanLyThietBi.ManagerBase;
using QuanLyThietBi.HamXuLy;
using QuanLyThietBi.Entity;


namespace QLTB.Forms
{
    public partial class FrmEditThietBi : DevComponents.DotNetBar.Office2007Form
    {


        public EventHandler ThemEventThietbi;


        public FrmEditThietBi()
        {
            InitializeComponent();
        }

       
        public FrmEditThietBi(string MaTB): this()
        {
            txtMaTB.Text = MaTB;
            //TBL_LoaiThietBiManager.CreateInstant().HienThiComboBoxLoaiThietBi(cbxLoaiTB);

            TBL_LoaiThietBiManager.CreateInstant().HienThiComboBoxLoaiThietBi(cbxLoaiTB);
            

            TBL_DonViTinhManager.CreateInstant().HienThiComboBoxDonViTinh(cbxDonvi);
            

            if (CheckIdExist(MaTB) == true)
                LoadDataUpdate(MaTB);

        }
        private bool CheckIdExist(string MaTB)
        {
            DataTable dt = TBL_ThietBiManager.CreateInstant().SelectByMaTB(MaTB);
            if (dt.Rows.Count > 0)
                return true;
            else
                return false;
        }

        private void LoadDataUpdate(string MaTB)
        {
            DataTable dtThietBi = TBL_ThietBiManager.CreateInstant().SelectByMaTB(MaTB);
            if (dtThietBi.Rows.Count > 0)
            {
                txtMaTB.Text = dtThietBi.Rows[0]["MaTB"].ToString();
                txtTenTB.Text = dtThietBi.Rows[0]["TenTB"].ToString();
                cbxLoaiTB.SelectedValue = dtThietBi.Rows[0]["MaLoaiTB"].ToString();
                cbxDonvi.SelectedValue = dtThietBi.Rows[0]["MaDonVi"].ToString();
                txtTTCauHinh.Text = dtThietBi.Rows[0]["ThongtinCauHinh"].ToString();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTroGiup_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Thông tin hỗ trợ kỹ thuật!");
        }

        private void btnApdung_Click(object sender, EventArgs e)
        {
            if (CheckIdExist(txtMaTB.Text))
            {
                UpdateThietBi(txtMaTB.Text);
            }
            else
            {
                InsertThietBi();
                HamXuLy GetID = new HamXuLy();
                string strThietBi = GetID.LayMaID("P_GetMaThietBi");
                txtMaTB.Text = strThietBi;
                txtTenTB.Text = "";
                txtTTCauHinh.Text = "";
                cbxLoaiTB.SelectedIndex = 0;
                cbxDonvi.SelectedIndex = 0;
               
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
           
            if (CheckIdExist(txtMaTB.Text)== true)
            {
               
                UpdateThietBi(txtMaTB.Text);
                this.Close();
            }
            else
            {
                InsertThietBi();
                this.Close();
            }
        }

        void InsertThietBi()
        {
           
            if (txtMaTB.Text != "" && txtTenTB.Text != "" && txtTTCauHinh.Text != "" && cbxLoaiTB.SelectedValue.ToString()!="None" && cbxDonvi.SelectedValue.ToString()!="None")
            {
                TBL_ThietBiEntity objThietbi = new TBL_ThietBiEntity();
                objThietbi.MaTB = txtMaTB.Text;
                objThietbi.TenTB = txtTenTB.Text;
                objThietbi.MaLoaiTB = cbxLoaiTB.SelectedValue.ToString();
                objThietbi.MaDonVi = cbxDonvi.SelectedValue.ToString();
                objThietbi.ThongTinCauHinh = txtTTCauHinh.Text;
                TBL_ThietBiManager.CreateInstant().Insert(objThietbi);

                if (ThemEventThietbi != null)
                    ThemEventThietbi(this, EventArgs.Empty);

            }
            else
            {
                MessageBoxEx.Show("Thông tin các trường dữ liệu không được rỗng!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void UpdateThietBi(string MaTB)
        {
            if (txtMaTB.Text != "" && txtTenTB.Text != "" && txtTTCauHinh.Text != "" && cbxLoaiTB.SelectedValue.ToString() != "None" && cbxDonvi.SelectedValue.ToString() != "None")
            {
                TBL_ThietBiEntity objThietbi = new TBL_ThietBiEntity();
                objThietbi.MaTB = txtMaTB.Text;
                objThietbi.TenTB = txtTenTB.Text;
                objThietbi.MaLoaiTB = cbxLoaiTB.SelectedValue.ToString();
                objThietbi.MaDonVi = cbxDonvi.SelectedValue.ToString();
                objThietbi.ThongTinCauHinh = txtTTCauHinh.Text;
                TBL_ThietBiManager.CreateInstant().Update(objThietbi);

                if (ThemEventThietbi != null)
                    ThemEventThietbi(this, EventArgs.Empty);

            }
            else
            {
                MessageBoxEx.Show("Thông tin các trường dữ liệu không được rỗng!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

      
    }
}